/*
 * Copyright 2017 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jess.arms.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.InflateException;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.gyf.immersionbar.ImmersionBar;
import com.jess.arms.R;
import com.jess.arms.base.delegate.IActivity;
import com.jess.arms.callback.EmptyCallback;
import com.jess.arms.callback.ErrorCallback;
import com.jess.arms.callback.LottieLoadingCallback;
import com.jess.arms.callback.TimeoutCallback;
import com.jess.arms.integration.cache.Cache;
import com.jess.arms.integration.cache.CacheType;
import com.jess.arms.integration.lifecycle.ActivityLifecycleable;
import com.jess.arms.mvp.IPresenter;
import com.jess.arms.mvp.IView;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.LoadingView;
import com.jess.arms.widget.onchangeview.IChangeLayoutViewInterfaces;
import com.jess.arms.widget.onchangeview.OnChangeLayoutView;
import com.kingja.loadsir.core.LoadService;
import com.trello.rxlifecycle2.android.ActivityEvent;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.subjects.BehaviorSubject;
import io.reactivex.subjects.Subject;

import static com.jess.arms.utils.Preconditions.checkNotNull;
import static com.jess.arms.utils.ThirdViewUtil.convertAutoView;

/**
 * author : Alex
 * e-mail : xys9786@outlook.com
 * date   : 2020/4/1220:36
 * desc   :
 * version: 1.0
 */
public abstract class BaseActivity<P extends IPresenter> extends AppCompatActivity implements IActivity, IView, ActivityLifecycleable, IChangeLayoutViewInterfaces {
    protected final String TAG = this.getClass().getSimpleName();

    private final BehaviorSubject<ActivityEvent> mLifecycleSubject = BehaviorSubject.create();

    private Cache<String, Object> mCache;

    private Unbinder mUnbinder;

    @Inject
    @Nullable
    protected P mPresenter;//如果当前页面逻辑简单, Presenter 可以为 null

    private OnChangeLayoutView mOnChangeLayoutView;

    @NonNull
    @Override
    public synchronized Cache<String, Object> provideCache() {
        if (mCache == null) {
            mCache = ArmsUtils.obtainAppComponentFromContext(this).cacheFactory().build(CacheType.ACTIVITY_CACHE);
        }
        return mCache;
    }

    @NonNull
    @Override
    public final Subject<ActivityEvent> provideLifecycleSubject() {
        return mLifecycleSubject;
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        View view = convertAutoView(name, context, attrs);
        return view == null ? super.onCreateView(name, context, attrs) : view;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mOnChangeLayoutView = new OnChangeLayoutView(this);
        try {
            int layoutResID = initView(savedInstanceState);
            if (layoutResID != 0) {
                addContent(layoutResID);
                setContentView(mOnChangeLayoutView);
            }
        } catch (Exception e) {
            if (e instanceof InflateException) throw e;
            e.printStackTrace();
        }
        initData(savedInstanceState);
        ImmersionBar.with(this).statusBarView(mOnChangeLayoutView.findViewById(R.id.v1)).init();
    }
    /**
     *  添加布局
     * @param layoutResID
     */
    private void addContent(int layoutResID) {
        mOnChangeLayoutView.setOnNetReload(this);
        mOnChangeLayoutView.isShowTitle(true);

        View content = View.inflate(this, layoutResID, null);
        if (content != null) {
            mOnChangeLayoutView.setContentView(content);
            ButterKnife.bind(this, mOnChangeLayoutView);
        }
    }
    /**
     * 标题
     * @return
     */
    public abstract String getContentTitle();

    /**
     *返回点击
     */
    public void onBackClick() {
        this.onBackPressed();
    }
    /**
     *标题右边点击
     */
    public void onRightClick() {

    }
    /**
     *  重新加载数据的方法
     * @param view
     */
    @Override
    public abstract void onNetReload(View view) ;

    /**
     *标题右边点击
     */
    @Override
    public String getRightString() {
        return "编辑";
    }

    /**
     * 是否注册evenbus
     * @return
     */
    @Override
    public boolean useEventBus() {
        return true;
    }

    /**
     * 是否使用fragment
     * @return
     */
    @Override
    public boolean useFragment() {
        return true;
    }
    /**
     * 是否显示返回键
     * @return
     */
    @Override
    public boolean isUseBack() {
        return true;
    }
    /**
     * 是否显示右边文字
     * @return
     */
    @Override
    public boolean isUseRight() {
        return false;
    }
    /**
     * 加载布局
     */
    @Override
    public void showLoadingLayout() {
        if (mOnChangeLayoutView != null) {
            mOnChangeLayoutView.showLoadingLayout();
        }
    }

    /**
     * 显示空白页面
     */
    @Override
    public void showEmpty() {
        if (mOnChangeLayoutView != null)
            mOnChangeLayoutView.showEmpty();
    }

    /**
     * 显示内容
     */
    @Override
    public void showContent() {
        if (mOnChangeLayoutView != null) {
            mOnChangeLayoutView.showContent();
        }
    }

    /**
     * 显示加载失败
     */
    @Override
    public void showFailure() {
        if (mOnChangeLayoutView != null) {
            mOnChangeLayoutView.showFailure();
        }
    }

    /**
     * 显示无网络
     */
    @Override
    public void showNoNetwork() {
        if (mOnChangeLayoutView != null) {
            mOnChangeLayoutView.showNoNetwork();
        }
    }

    /**
     * 加载超时
     */
    @Override
    public void showTimeOut() {
        if (mOnChangeLayoutView != null) {
            mOnChangeLayoutView.showTimeOut();
        }
    }


    /**
     * 隐藏加载框
     */
    @Override
    public void hideLoading() {
        mOnChangeLayoutView.closeLoadingDialog();
    }

    /**
     * 显示加载框
     */
    @Override
    public void showLoading() {
        mOnChangeLayoutView.showLoadingDialog();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null && mUnbinder != Unbinder.EMPTY)
            mUnbinder.unbind();
        this.mUnbinder = null;

        if (mPresenter != null)
            mPresenter.onDestroy();//释放资源
        this.mPresenter = null;

    }
}

